package com.university.evaluation.util;

import com.university.evaluation.entity.User;
import com.university.evaluation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * 用户上下文工具类
 * 用于获取当前登录用户的信息
 */
@Component
public class UserContextUtil {

    private static UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        UserContextUtil.userService = userService;
    }

    /**
     * 获取当前登录用户的用户名
     * @return 用户名，如果未登录返回null
     */
    public static String getCurrentUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserDetails) {
                return ((UserDetails) principal).getUsername();
            } else if (principal instanceof String) {
                return (String) principal;
            }
        }
        return null;
    }

    /**
     * 获取当前登录用户的ID
     * @return 用户ID，如果未登录或无法解析返回null
     */
    public static Long getCurrentUserId() {
        String username = getCurrentUsername();
        if (username != null) {
            try {
                // 如果用户名是数字，直接转换为Long
                return Long.parseLong(username);
            } catch (NumberFormatException e) {
                // 如果用户名不是数字，通过UserService根据用户名查询用户ID
                if (userService != null) {
                    User user = userService.findByUsername(username);
                    return user != null ? user.getId() : null;
                }
                return null;
            }
        }
        return null;
    }

    /**
     * 获取当前认证信息
     * @return Authentication对象，如果未登录返回null
     */
    public static Authentication getCurrentAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 检查用户是否已登录
     * @return true表示已登录，false表示未登录
     */
    public static boolean isAuthenticated() {
        Authentication authentication = getCurrentAuthentication();
        return authentication != null && authentication.isAuthenticated() 
               && !"anonymousUser".equals(authentication.getPrincipal());
    }

    /**
     * 获取当前用户的角色信息
     * @return 角色字符串，如果未登录返回null
     */
    public static String getCurrentUserRole() {
        Authentication authentication = getCurrentAuthentication();
        if (authentication != null && authentication.getAuthorities() != null) {
            return authentication.getAuthorities().stream()
                    .findFirst()
                    .map(authority -> authority.getAuthority())
                    .orElse(null);
        }
        return null;
    }
}
